package com.gz.service;
import com.gz.common.Message;
import com.gz.common.MessageType;
import com.gz.utilis.Utility;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class ClientConnectServerThread extends Thread{
    private Socket socket;
    public ClientConnectServerThread(Socket socket) {
        this.socket = socket;
    }

    public Socket getSocket() {
        return socket;
    }

    public void run() {
        Scanner scanner = new Scanner(System.in); // 在循环外部创建 Scanner 对象
        while(true){
//            System.out.println("客户端进程等待服务器发送的消息");
            try {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();//如果服务器没有发送，线程就阻塞等待

                //如果读取到的是服务端返回的在线服务列表
                if(message.getMestype().equals(MessageType.MESSAGE_RET_ONLINE_USER)){
                    String[] onlineUsers=message.getContent().split(" ");
                    System.out.println("\n=======当前用户在线列表=======");
                    for(int i=0;i<onlineUsers.length;i++){
                        System.out.println("用户: "+onlineUsers[i]);
                    }
                } else if (message.getMestype().equals(MessageType.MESSAGE_COMM_MES)) {
                    System.out.println(message.getSendTime());
                    System.out.println(message.getSender()+"对你说: "+message.getContent());

                } else if (message.getMestype().equals(MessageType.MESSAGE_TO_ALL_MES)) {
                    System.out.println(message.getSendTime());
                    System.out.println(message.getSender()+" 对大家说(群消息): "+message.getContent());
                } else if (message.getMestype().equals(MessageType.MESSAGE_FILE_MES)) {
                    System.out.print("是否接收"+message.getSender()+"发给你的文件(Y/N,先回车一下)");
                    char c;
                    for (; ; ) {//无限循环
                        //在这里，将接受到字符，转成了大写字母
                        //y => Y n=>N
                        String str = scanner.next().toUpperCase();
                        if (!str.isEmpty()) {
                            c = str.charAt(0);
                            if (c == 'Y' || c == 'N') {
                                break;
                            }
                        }else {
                            System.out.print("选择错误，请重新输入(Y/N)：");
                        }
                    }
                    if(c=='Y'){
                        FileOutputStream fos = new FileOutputStream(message.getFilepath());
                        fos.write(message.getFilebytes());
                        fos.close();
                        System.out.println("接收成功");
                    }
                    else
                        System.out.println("已取消");

                } else if (message.getMestype().equals(MessageType.MESSAGE_SER_NEWS)) {
                    System.out.println(message.getSendTime()+" 服务端推送新闻: "+message.getContent());


                } else{
                    System.out.println("是其他类型的message，暂时不处理");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
